﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Northwind.NET.EDM.FE.WinForms.Controls
{
    public sealed class ControlsFactory
    {
        private static UserControl getControlInstance(string nameSpace, string controlName)
        {
            try
            {
                string typeName = string.Format(nameSpace, controlName);
                Type t = Type.GetType(typeName);
                UserControl controlInstance = Activator.CreateInstance(t) as UserControl;
                return controlInstance; 
            }
            catch { }

            return null;
        }
        public static UserControl CreateControlInstance(string controlName)
        {
            //string typeName = string.Format("Northwind.NET.EDM.FE.WinForms.Controls.{0}", controlName);
            //Type t = Type.GetType(typeName);
            //UserControl controlInstance = Activator.CreateInstance(t) as UserControl;

            UserControl controlInstance = getControlInstance("Northwind.NET.EDM.FE.WinForms.Controls.{0}", controlName);

            if (controlInstance == null)
            {
                controlInstance = getControlInstance("Northwind.NET.EDM.Reports.{0}", controlName);
            }

            if (controlInstance == null)
            {
                throw new ApplicationException(
                    string.Format(Northwind.NET.Settings.Resources.DecodeResourceString(Northwind.NET.Settings.Resources.UnknownUserControlName),
                                    controlName));
            }
            return controlInstance; 
        }

        public class ControlNames
        {
            public const string CustomersUserControlName = "CustomersUserControl";
            public const string SuppliersUserControlName = "SuppliersUserControl";
            public const string CategoriesUserControlName = "CategoriesUserControl";
            public const string ProductsUserControlName = "ProductsUserControl";
            public const string OrdersUserControlName = "OrdersUserControl";
            public const string CustomerOrdersUserControlName = "CustomerOrdersUserControl";
            public const string EmployeesUserControlName = "EmployeesUserControl";
            public const string CustomersPhoneListUserControlName = "CustomersPhoneListUserControl";
            public const string QuarterlyOrdersUserControlName = "QuarterlyOrdersUserControl";
            public const string ReportViewingUserControlName = "ReportViewingUserControl";
                                                                
            public const string Undefined = "UndefinedUserControl";
        }
    }
}
